<?php
namespace app\manage\controller;
use think\Controller;
use think\Db;

abstract class Base extends Controller{
	protected $mListUrl;
	protected $mDbModel = 'AminoStandard';
	protected $mModelName = 'standard';

    public function initialize(){
		$this->mListUrl = url('manage/'.$this->mModelName.'/index');
	}
	
	public function index(){
		
		$db = Db::name($this->mDbModel);
		$result_list = $db->select();
		$this->assign($this->mModelName.'s', $result_list);
        return $this->fetch();
	}
	
	public function pre_edit($id){
		if(is_numeric($id)){
			$db = Db::name($this->mDbModel);
			$result = $db->where('id='.$id)->find();
			$this->assign($this->mModelName, $result);
            return $this->fetch();
		}else{
			$this->error('参数非法，无法进行操作', $this->mListUrl);
		}
	}
	
	public function edit(){
		$data = input('post.');
		$id = $data['id'];
		if(is_numeric($id)){
			$db = Db::name($this->mDbModel);
		    $db->where('id='.$id)->save($data);
		    $this->success('更新成功', $this->mListUrl);
		}else{
			$this->error('ID参数非常，无法更新');
		}
	}

	public function pre_add(){
	    return $this->fetch();
    }
	public function add(){
		$data = input('post.');
		$single = $data['single'];
		$full = $data['full'];
		if(is_null($full) || strlen($full)==0){
			$full = $single;
			$data['full'] = $single;
		}
		
		$db = Db::name($this->mDbModel);
		$db->create($data);
		$db->add();
		$this->success('新增成功', $this->mListUrl);
	}
	/*
	public function delete($id){
		if(is_numeric($id)){
			
			$log_db = Db:name('base_log');
			$log_data = ['title'=>$this->mDbModel, 'message'=>get_client_ip.'删除当前记录'.$id, 'create_date'=>date()];
			$log_db->create($log_data);
			$log_db->add();
			
			$db = Db::name($this->mDbModel);
			$result = $db->where('id='.$id)->delete();
			if($result==false || $result==0){
				$this->error('参数错误，无法删除');
			}else{
				$this->success('删除成功', $this->mListUrl);
			}
		}else{
			$this->error('参数错误，无法删除');
		}
	}
	*/
	function get_client_ip($type = 0) {
	    $type       =  $type ? 1 : 0;
	    static $ip  =   NULL;
	    if ($ip !== NULL) return $ip[$type];
	    if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下，获取客户端真实IP
	        $ip=$_SERVER['HTTP_X_REAL_IP'];     
	    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
	        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
	    }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
	        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
	        $pos    =   array_search('unknown',$arr);
	        if(false !== $pos) unset($arr[$pos]);
	        $ip     =   trim($arr[0]);
	    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
	        $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
	    }else{
	        $ip=$_SERVER['REMOTE_ADDR'];
	    }
	    // IP地址合法验证
	    $long = sprintf("%u",ip2long($ip));
	    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
       }
}
}
